package cn.chinaunicom.sdsi.gacx.sys.service.serviceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaffOrg;
import cn.chinaunicom.sdsi.gacx.sys.dao.mybatis.GaMainInfoMapper;
import cn.chinaunicom.sdsi.gacx.sys.service.SysMainService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author chenxl 20170515
 *
 */
@Service
public class SysMainServiceImpl extends BaseService implements SysMainService {

	@Autowired
	private GaMainInfoMapper gaMainInfoMapper;

	/**
	 * 获取首页
	 */
	@Override
	public JSONObject getMainInfo() {
		SysStaff staff = getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
		SysStaffOrg sso = getSessionCache(R.UserSign.SIGNIN_USER_STAFFORG_SESSION);
		int staffId = staff.getStaffId();
		String staffKind = staff.getStaffKind();
		String personType = staff.getPersonType();
		// staffId = 10002;
		// staffKind = "1";
		// int sysOrgId =123;
		int sysOrgId = sso.getStaffOrgId();

		JSONObject rObj = new JSONObject();

		// 获取图表数据 0：待提交 1：初审 2:复审 3：备案 4：终审 5：完结
		List barXAxisList = new ArrayList();
		List barSeriesList = new ArrayList();
		JSONArray pieA = new JSONArray();

		// 获取代办任务
		List taskList = gaMainInfoMapper.getToDoTaskList1(sysOrgId);
		JSONArray array = new JSONArray();
		for (int i = 0; i < taskList.size(); i++) {
			Map tmp = (Map) taskList.get(i);
			JSONObject obj = new JSONObject();
			obj.put("taskName", tmp.get("taskName"));
			obj.put("procInstId", tmp.get("proc_inst_id"));
			array.add(obj);
		}
		rObj.element("toDoTaskInfo", array);

		if (staffKind.equals("1")) {// 公安人员

			// 获取待备案数量
			Map taskBzCount = gaMainInfoMapper.getToDoBaTaskCount(staffId);
			String count = taskBzCount.get("taskCount") == null ? "0" : taskBzCount.get("taskCount").toString();
			String dDate = taskBzCount.get("dDate").toString();
			rObj.element("taskBzCount", count);
			rObj.element("dDate", dDate);

			List chartDataList = gaMainInfoMapper.getChartDataList(staffId);

			String[] barXAxisData = { "待提交", "初审", "复审", "备案", "终审", "完结" };
			String[] barSeriesData = new String[6];

			for (int i = 0; i < 6; i++) {
				if (chartDataList.size() > 0) {
					for (int j = 0; j < chartDataList.size(); j++) {
						Map tmp = (Map) chartDataList.get(j);
						JSONObject obj = new JSONObject();
						String status = (String) tmp.get("flow_status");
						if (String.valueOf(i).equals(status)) {
							barSeriesData[i] = tmp.get("count").toString();
							obj.put("name", barXAxisData[i]);
							obj.put("value", tmp.get("count"));
							pieA.add(obj);
							break;
						}
						if (j == chartDataList.size() - 1) {
							barSeriesData[i] = "0";
							obj.put("name", barXAxisData[i]);
							obj.put("value", "0");
							pieA.add(obj);
						}
					}
				} else {
					barSeriesData[0] = "0";
					JSONObject obj = new JSONObject();
					obj.put("name", barXAxisData[i]);
					obj.put("value", "0");
					pieA.add(obj);
				}
			}
			barXAxisList = Arrays.asList(barXAxisData);
			barSeriesList = Arrays.asList(barSeriesData);
		} else {// 运维及网安人员

			// 获取待备案数量
			Map taskBzCount = gaMainInfoMapper.getToDoBaTaskCount(sysOrgId);
			String count = taskBzCount.get("taskCount") == null ? "0" : taskBzCount.get("taskCount").toString();
			String dDate = taskBzCount.get("dDate").toString();
			rObj.element("taskBzCount", count);
			rObj.element("dDate", dDate);

			List chartDataList = gaMainInfoMapper.getChartDataList1(sysOrgId);

			String[] barXAxisData = { "待办", "已办" };
			String[] barSeriesData = new String[2];

			Map tmp = (Map) chartDataList.get(0);
			barSeriesData[0] = tmp.get("count").toString();
			JSONObject obj = new JSONObject();
			obj.put("name", barXAxisData[0]);
			obj.put("value", tmp.get("count"));
			pieA.add(obj);

			tmp = (Map) chartDataList.get(1);
			barSeriesData[1] = tmp.get("count").toString();
			obj = new JSONObject();
			obj.put("name", barXAxisData[1]);
			obj.put("value", tmp.get("count"));
			pieA.add(obj);

			barXAxisList = Arrays.asList(barXAxisData);
			barSeriesList = Arrays.asList(barSeriesData);
		}

		rObj.element("barXAxisData", barXAxisList);
		rObj.element("barSeriesData", barSeriesList);
		rObj.element("pieData", pieA);
		return rObj;
	}

}